/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrder;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderDetail;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderDetailDao;

/**
 * 用户订单管理Service
 * @author 罗天文
 * @version 2018-08-22
 */
@Service
@Transactional(readOnly = true)
public class HkShopUserOrderService extends CrudService<HkShopUserOrderDao, HkShopUserOrder> {

	@Autowired
	private HkShopUserOrderDetailDao hkShopUserOrderDetailDao;
	
	public HkShopUserOrder get(String id) {
		HkShopUserOrder hkShopUserOrder = super.get(id);
		hkShopUserOrder.setHkShopUserOrderDetailList(hkShopUserOrderDetailDao.findList(new HkShopUserOrderDetail(hkShopUserOrder)));
		return hkShopUserOrder;
	}
	
	public List<HkShopUserOrder> findList(HkShopUserOrder hkShopUserOrder) {
		return super.findList(hkShopUserOrder);
	}
	
	public Page<HkShopUserOrder> findPage(Page<HkShopUserOrder> page, HkShopUserOrder hkShopUserOrder) {
		return super.findPage(page, hkShopUserOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopUserOrder hkShopUserOrder) {
		super.save(hkShopUserOrder);
		for (HkShopUserOrderDetail hkShopUserOrderDetail : hkShopUserOrder.getHkShopUserOrderDetailList()){
			if (hkShopUserOrderDetail.getId() == null){
				continue;
			}
			if (HkShopUserOrderDetail.DEL_FLAG_NORMAL.equals(hkShopUserOrderDetail.getDelFlag())){
				if (StringUtils.isBlank(hkShopUserOrderDetail.getId())){
					hkShopUserOrderDetail.setOrder(hkShopUserOrder);
					hkShopUserOrderDetail.preInsert();
					hkShopUserOrderDetailDao.insert(hkShopUserOrderDetail);
				}else{
					hkShopUserOrderDetail.preUpdate();
					hkShopUserOrderDetailDao.update(hkShopUserOrderDetail);
				}
			}else{
				hkShopUserOrderDetailDao.delete(hkShopUserOrderDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopUserOrder hkShopUserOrder) {
		super.delete(hkShopUserOrder);
		hkShopUserOrderDetailDao.delete(new HkShopUserOrderDetail(hkShopUserOrder));
	}
	
}